package ufabc.edu.helppo.rpc.command.impl;

import static ufabc.edu.helppo.rpc.RPCOpenSimMethods.ADMIN_REGION_QUERY;
import static ufabc.edu.helppo.rpc.RPCOpenSimParameters.REGION_UUID;
import static ufabc.edu.helppo.rpc.RPCOpenSimParameters.SERVER_PASSWORD;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;

import ufabc.edu.helppo.rpc.command.RPCCommand;
import ufabc.edu.helppo.rpc.singleton.HelppoProperties;
import ufabc.edu.helppo.rpc.singleton.RpcClientSingleton;

public class RegionQueryRPCCommandImpl extends RPCCommand {
	private String uuid;

	@Override
	public boolean execute() throws XmlRpcException, IOException {

		XmlRpcClient client = RpcClientSingleton.getInstance();
		configure(client);
		HashMap<String, Object> retorno = (HashMap<String, Object>) client
				.execute(ADMIN_REGION_QUERY, parameter());

		String health = ""+ retorno.get("health");
		
		try {
			if(health.equalsIgnoreCase("1")) {
				return true;
			} else {
				return false;
			}
		} catch(Exception e) {
			return false;
		}
	}

	private List<Hashtable<String, String>> parameter() throws IOException {
		Hashtable<String, String> parameters = new Hashtable<String, String>();

		ResourceBundle properties = HelppoProperties.getInstance();

		parameters.put(REGION_UUID, uuid);
		parameters.put(SERVER_PASSWORD, properties.getString(SERVER_PASSWORD));

		List<Hashtable<String, String>> list = new ArrayList<Hashtable<String, String>>();
		list.add(parameters);

		return list;
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}
}
